package com.hookall.hellohook;

import android.os.Bundle;
import android.util.Log;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class HookTest implements IXposedHookLoadPackage {

    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
//        XposedBridge.log("Loaded app: " + loadPackageParam.packageName);

        if (loadPackageParam.packageName.equals("com.hookall.hellohook")) {
            Class clazz = loadPackageParam.classLoader.loadClass("com.hookall.hellohook.MainActivity");

            XposedHelpers.findAndHookMethod(clazz, "toastMessage",
                    String.class,
                    int.class,
                    new XC_MethodHook() {
                        // hooked 方法执行前执行
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            // 修改参数
                            param.args[1] = 200;
                            super.beforeHookedMethod(param);
                        }
                        // hooked 方法执行后执行
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            // 获取方法执行结果
                            Log.i("afterHookedMethod", param.getResult().toString());
                        }
            });
        }

    }
}
